using UnityEngine;
using System.Collections;

public class FadeInScene : MonoBehaviour {

    public Transform[] rooms;
    FadeDemo[] allFades;
    public CharacterCollision collision;
    int i;

	void Start () 
    {
        rooms = new Transform[GameObject.FindGameObjectWithTag("Room").transform.childCount];
        allFades = new FadeDemo[rooms.Length];

        foreach (Transform currentTransform in GameObject.FindGameObjectWithTag("Room").transform)
        {
            rooms[i] = currentTransform;
            i++;
        }
        allFades[0] = gameObject.AddComponent("FadeDemo") as FadeDemo;
        addCollision(rooms[0]);

        for (int i = 1; i < rooms.Length; i++)
        {
            allFades[i] = gameObject.AddComponent("FadeDemo") as FadeDemo;
            allFades[i].FadeOut(rooms[i]);
            addCollision(rooms[i]);
        }

        
	}

    void Update()
    {
        if (Input.GetKeyDown("p"))
            BlackCube("_Room_01");
    }

    public void BlackCube(string currentRoom)
    {

        for (int i = 0; i < rooms.Length; i++)
        {
            if (currentRoom != rooms[i].name)
            {
                CompleteFadeIn a = gameObject.AddComponent("CompleteFadeIn") as CompleteFadeIn;
                a.FadeIn(rooms[i]);
            }
        }
    }

    public void ReturnBlackCube(string currentRoom)
    {
        for (int i = 0; i < rooms.Length; i++)
        {
            if (currentRoom != rooms[i].name)
            {
                CompleteFadeOut a = gameObject.AddComponent("CompleteFadeOut") as CompleteFadeOut;
                a.FadeOut(rooms[i]);
            }
        }
    }

    void addCollision(Transform t)
    {
        if (this.collision == null)
            if(GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterCollision>())
                this.collision = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterCollision>();

        foreach (Transform currentTransform in t.transform)
        {
            if (currentTransform.name == "Floor")
            {
                if (currentTransform.GetComponent<FlashColor>())
                {
                    this.collision.target = currentTransform;
                }

                if (currentTransform.GetComponent<BoxCollider>() == null)
                    currentTransform.gameObject.AddComponent<BoxCollider>();

                RoomInfo currentInfo = currentTransform.gameObject.AddComponent("RoomInfo") as RoomInfo;
                currentInfo.Father = t;

                currentTransform.GetComponent<BoxCollider>().isTrigger = true;
                currentTransform.GetComponent<BoxCollider>().size = new Vector3(0.9f, 0.9f, 1000);
            }
        }
    }

}
